rootIden = [];
addSemiColon = false;
state = null;

Op = {
  EQ: "==",
  SEQ: "skip"
};

Local = {
    Assignment: function() {
        return ' = ';
    },
    BlockStart: function() {
        return '{\n';
    },
    BlockEnd: function() {
        return '}';
    },
    Else: function() {
        return ' else ';
    },
    End: function() {
        return '});\n';
    },
    IfCondEnd: function() {
        return ' )\n';
    },
    IfCondStart: function() {
        return 'if( ';
    },
    Root: function() {
        return '$root$';
    },
    Var: function(value) {
        return value;
    },
    OutputVar: function(value, varNr) {
        var script;
        if( rootIden.indexOf(value) != '-1' )
            script = Local.Root() + '.getString("g' + varNr + '")';
        
        else
            script = value + '.getString("g' + varNr + '")';
        
        return script;
    },
    LoopStart: function(name, root) {
        if( !root ) root = '$result$';
        return root+'.getIteration("' + name + '").forEach(function(' + name + '){\n';
    },
    PostfixOp: function(value) {
        return value + " ";
    },
    OptionalSemiColon: function(code) {
        var lastString = code[code.length-1];
        if( !lastString ) return false;
        
        code.push(Local.GetOptionalSemiColon(lastString));
    },
    GetOptionalSemiColon: function(str) {
        var lastChar = str.charAt(str.length-1);
        if( lastChar === ' ') lastChar = str.charAt(str.length-2);
        if( lastChar === ' ') return '';
        if(     lastChar !== '('
             && lastChar !== '{'
             && lastChar !== ';'
             && lastChar !== '\n') return ';\n';

        return '';
    }
};

Remote = {
    Root: function() {
        return 'root';
    },
    Var: function(value) {
        if( rootIden.indexOf(value) != '-1' )
            return Remote.Root();
        return value;
    },
    OutputVar: function(value, varNr) {
        var script = "";

        if( addSemiColon ) {
            script = script + '; ';
        }

        if( rootIden.indexOf(value) != '-1' )
            script = script + 'g' + varNr + ':' + Remote.Root();
        
        else 
            script = script + 'g' + varNr + ':' + value;
        
        addSemiColon = true;
        return script;
    },
    LoopStart: function(name) {
        if( addSemiColon ) {
            addSemiColon = false;
            return '; for ' + name + ' in ';
        }
        return 'for ' + name + ' in ';
    },
    LoopBlock: function() {
        return ' do (';
    },
    End: function() {
        // Enorme aliasing op state hier, moet beter..
        // var lastString = state.code.remote[state.code.remote.length-1];
        // console.log("laststring:"+lastString);
        //if( !lastString ) return false;
        addSemiColon = false;
        return ') end; ';
    },
    IfStart: function() {
        if( addSemiColon ) {
            addSemiColon = false;
            return '; if (';
        }
        return 'if ('
    },
    IfBlock: function() {
        return ') then (';
    },
    VarDecl: function(name) {
        return 'var ' + name + ' = (';
    },
    varDeclEnd: function() {
        addSemiColon = true;
        return ')';
    },
    PostfixOp: function(value) {
        if( addSemiColon ) {
            addSemiColon = false;
            return "; "+ value + " ";
        }
        return value + " ";
    }/*,
    OptionalSemiColon: function(code) {
        return;
        var lastString = code[code.length-1];
        console.log("laststring:"+lastString);
        if( !lastString ) return false;
        
        code.push(Remote.GetOptionalSemiColon(lastString));
    },
    GetOptionalSemiColon: function(str) {
        var lastChar = str.charAt(str.length-1);
        if( lastChar === ' ') lastChar = str.charAt(str.length-2);
        if( lastChar === ' ') return '';
        if( lastChar !== '('  && lastChar !== ';') return '; ';
        
        return '';
    }*/
}

setRootIden = function(r) {
    rootIden = r;
}

setState = function(s) {
    state = s;
}

exports.setRootIden = setRootIden;
exports.setState = setState;
exports.Op = Op;
exports.Local = Local;
exports.Remote = Remote;